package com.yy.weixinpay.task;

import com.yy.weixinpay.entity.OrderInfo;
import com.yy.weixinpay.enums.PayType;
import com.yy.weixinpay.service.AliPayService;
import com.yy.weixinpay.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class AliPayTask {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private AliPayService aliPayService;

    /**
     * 支付宝查单的定时任务
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirm(){
        log.info("支付宝查单，超过五分钟没有支付的订单");
        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5, PayType.ALIPAY.getType());
        orderInfoList.forEach(orderInfo -> {
            String orderNo = orderInfo.getOrderNo();
            log.warn("超时订单：{}",orderNo);
            aliPayService.checkOrderStatus(orderNo);
        });

    }
}
